using System;
using miPics.Common;


public partial class UserControl_ImageList : miPics.BaseUserControl.BaseUserControl
{
	private short _GalleryID;

	// event handler to pass gallery details back to parent controls/pages
	public event EventHandler ImagesLoaded;
	
	protected void Page_Load(object sender, EventArgs e)
	{
		// hide create image link
		_CreateImage.Visible = false;

		// get querystring values
		this._GalleryID = ToolKit.ReturnShort(Request.QueryString["id"]);

		if (_GalleryID > 0)
		{
			// fetch gallery name and description
			miPicsDataAccessTableAdapters.GalleryItemTableAdapter GalleryItemTableAdapter = new miPicsDataAccessTableAdapters.GalleryItemTableAdapter();
			miPicsDataAccess.GalleryItemDataTable GalleryItem = GalleryItemTableAdapter.GetData(GalleryID,this._LoginMode);

			// was a gallery item loaded?
			if (GalleryItem.Count == 1)
			{
				if (this._LoginMode)
				{
					// setup create image link
					_CreateImage.Visible = true;
					_CreateImageLink.HRef = "~/Administration/CreateImage.aspx?id=" + GalleryID.ToString();
				}

				// set gallery title and description
				miPicsDataAccess.GalleryItemRow GalleryRow = (miPicsDataAccess.GalleryItemRow)GalleryItem.Rows[0];

				_GalleryTitle.InnerText = GalleryRow.Name;
				_GalleryDescription.InnerText = GalleryRow.Description;

				// fire ImagesLoaded event
				if (this.ImagesLoaded != null)
					this.ImagesLoaded(this, EventArgs.Empty);
			
				// fetch gallery images
				miPicsDataAccessTableAdapters.ImageListTableAdapter ImageListTableAdapter = new miPicsDataAccessTableAdapters.ImageListTableAdapter();
				miPicsDataAccess.ImageListDataTable ImageListDataTable = ImageListTableAdapter.GetData(this._GalleryID,this._LoginMode);

				// cycle images and create user controls
				foreach (miPicsDataAccess.ImageListRow ImageItem in ImageListDataTable)
				{
					UserControl_ImageListItem ListItem = (UserControl_ImageListItem)LoadControl("~/UserControl/ImageListItem.ascx");

					ListItem.ImageID = ImageItem.ImageID;
					ListItem.Name = ImageItem.Name;
					ListItem.Thumbnail = ImageItem.Thumbnail;
					ListItem.ThumbnailWidth = ImageItem.ThumbnailWidth;
					ListItem.ThumbnailHeight = ImageItem.ThumbnailHeight;

					ListItem.EditMode = this._LoginMode;

					_ImagePlaceHolder.Controls.Add(ListItem);
				}
			}
		}
	}

	public short GalleryID
	{
		get { return this._GalleryID; }
	}

	public string GalleryTitle
	{
		get { return _GalleryTitle.InnerText; }
	}
}
